package demo.view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableModel;

import demo.dao.ReaderDao;
import demo.entity.Reader;

public class ReaderShow extends JPanel{
	private JTable table;
	private String[] tableHeader = { "学号", "密码", "姓名", "性别","生日","专业","年级","电话","邮箱" };
	Object[][] readerData = { { "001", "123","张三", "男", "2010-08-06","信管","20","12345678908","12345" }, { "002","123", "李四", "女", "2011-09-09","信管","20","12345678908","12345" } };
	
	public ReaderShow() {
		setLayout(new BorderLayout(0, 0));

		table = new JTable(readerData, tableHeader);

		JScrollPane scrollPane = new JScrollPane(table);
		add(scrollPane, BorderLayout.CENTER);

		JPanel panel = new JPanel();
		add(panel, BorderLayout.SOUTH);
		
		/*JButton btnSelect = new JButton("查询");
		btnSelect.setFont(new Font("",Font.BOLD,15));
		btnSelect.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				loadDataToTable();
			}
		});*/
		
		JButton btnRefresh = new JButton("刷新");
		btnRefresh.setFont(new Font("",Font.BOLD,15));
		btnRefresh.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				loadDataToTable();
			}
		});
		panel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
		//panel.add(btnSelect);
		panel.add(btnRefresh);
		
		

		JLabel lblNewLabel = new JLabel("读者列表");
		lblNewLabel.setFont(new Font("",Font.BOLD,25));
		//lblNewLabel.setForeground(Color.blue);
		add(lblNewLabel, BorderLayout.NORTH);
		lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
		loadDataToTable();
	}
	
	private void loadDataToTable() {
		// 生成一个StudentDao对象，调用它的findAll方法查询reader表的所有数据，并以List的形式返回。
		List<Reader> readerList = new ReaderDao().findAll();

		if (readerList != null) {// 如果列表不为空

			// 将列表转换成二维对象数组
			readerData = new Object[readerList.size()][tableHeader.length];
			for (int i = 0; i < readerData.length; i++) {
				readerData[i][0] = readerList.get(i).getId();
				readerData[i][1] = readerList.get(i).getPassword();
				readerData[i][2] = readerList.get(i).getName();
				readerData[i][3] = readerList.get(i).getSex();
				readerData[i][4] = readerList.get(i).getBirth();
				readerData[i][5] = readerList.get(i).getPro();
				readerData[i][6] = readerList.get(i).getGrade();
				readerData[i][7] = readerList.get(i).getTel();
				readerData[i][8] = readerList.get(i).getEmail();
			}
			DefaultTableModel defaultTableModel = new DefaultTableModel(readerData, tableHeader);// 生成数据模型
			table.setModel(defaultTableModel);// 设置表格的数据模型
			table.revalidate();
			table.repaint();
		}
	}
	
	
	
}
